(ns liberator-sample.core.tutorial.conditional-requests
  (:require [liberator.core :refer [defresource resource]]
            [liberator.dev :refer [wrap-trace]]
            [compojure.core :refer [defroutes ANY]]
            [ring.middleware.params :refer [wrap-params]]
            [org.httpkit.server :as server])
  (:use clojure.pprint))

(declare app)
(defroutes app
           (ANY "/timehop" []
                (resource
                  :available-media-types ["text/plain"]
                  :last-modified (do (prn "last-modified")
                                     (* 10000 (long (/ (System/currentTimeMillis) 10000))))
                  :handle-ok (fn [_]
                               (format "It's now %s", (java.util.Date.)))))
           (ANY "/changetag" []
                (resource
                  :available-media-types ["text/plain"]
                  :etag (let [i (int (mod (/ (System/currentTimeMillis) 10000) 10))]
                          (.substring "abcdefghijklmnopqrst" i (+ i 10)))
                  :handle-ok (format "It's now %s" (java.util.Date.)))))

(server/run-server app {:port 3333})